package org.afun.csms_android.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.afun.csms_android.R;
import org.afun.csms_android.adapter.ShowMyScoreAdapter;
import org.afun.csms_android.adapter.TeacherShowCourseAdapter;
import org.afun.csms_android.domain.Course;
import org.afun.csms_android.domain.Stu2Score;
import org.afun.csms_android.domain.Student;
import org.afun.csms_android.domain.Teacher;
import org.afun.csms_android.domain.javaBean.SS;
import org.afun.csms_android.domain.javaBean.TS;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;


public class StudentShowScore extends Fragment {
    private static Context context;
    private static RecyclerView recyclerView;
    private static TextView emptyText;
    private static Student student;

    private static List<SS> myScoreList;


    public StudentShowScore(Context context, Student student) {
        StudentShowScore.context = context;
        StudentShowScore.student = student;
    }

    private static void showMsg() {
        MyTableOperator myTableOperator = new MyTableOperator(context);
        TableOperator tableOperator = myTableOperator.getTableOperator();
        myScoreList = tableOperator.showMyScore(student);

        if (myScoreList.size() == 0) {
            emptyText.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            emptyText.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(layoutManager);

            ShowMyScoreAdapter showMyScoreAdapter = new ShowMyScoreAdapter(myScoreList, context);
            recyclerView.setAdapter(showMyScoreAdapter);

            DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), layoutManager.getOrientation());
            recyclerView.addItemDecoration(dividerItemDecoration);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_student_show_score, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        emptyText = (TextView) view.findViewById(R.id.empty_text);
        showMsg();
        return view;
    }

    public void onResume() {
        super.onResume();
        StudentShowScore.showMsg();
    }

    public static void update() {
        StudentShowScore.showMsg();
    }
}